﻿/*监测数据横向污染物的折线图*/
function Analyse(container, config) {
    this.timePoint = [];

    this.pollutantName = [];
    this.monitorValue = [];

    this.height = config["height"] || null;
    this.width = config["width"] || null;
    this.xwidth = config["xwidth"] || 60;
    this.plotLines = config["plotLines"] || null;

    this.title = config["title"] || "";
    this.useDefaultTip = config["useDefaultTip"] || false;
    this.doubleAxis = config["doubleAxis"] || false;
    this.coAxis = config["coAxis"] || false;
    this.yAxis = [];

    this.container = container;
}

Analyse.prototype.tooltipFormatter = function (tooltip) {
    var ret = '<middle>' + tooltip.x + '</middle><br>';
    ret += '<table>';

    Highcharts.each(tooltip.points, function (point) {
        var series = point.series;
        ret += '<tr><td><span style="color:' + series.color + '">\u25CF</span> ' + series.name + '</td><td>' + Highcharts.pick(point.point.value, point.y) + '</td></tr>';
    });

    ret += '</table>';

    return ret;

    var series = tooltip.point.series, point = tooltip.point;
    return series.name + ":" + Highcharts.pick(point.value, point.y);
};

Analyse.prototype.titleFormatter = function () {
    var _ = this;
    return pollutantNameSubTransform(_.container) + '过去14天历史变化图' || '过去14天历史变化图';
}


Analyse.prototype.getChartOptions = function () {
    var _ = this;
    return {
        chart: {
            renderTo: _.container,
            height: _.height,
            width: _.width
        },
        title: {
            useHTML: true,
            text: _.titleFormatter()
        },
        credits: {
            enabled: false
        },
        xAxis: [{
            categories: _.timePoint
        }],
        yAxis: {
            min: 0,
            title: {
                text: null
            },
            labels: {
                style: {
                    fontSize: '10px'
                }
            },
            plotLines: _.plotLines
        },
        tooltip: {
            shared: true,
            //crosshairs: true,
            crosshairs: [
              {
                width: 1,
              }
            ],
            useHTML: true,
            formatter: function () {
                return _.tooltipFormatter(this);
            }
        },
        series: [{
            name: pollutantNameSubTransform(_.container) + "日均值",
            color: '#434348',
            type: 'spline',
            data: _.monitorValue
        }]


    }
};

Analyse.prototype.onChartLoad = function () {
};

Analyse.prototype.createChart = function () {
    var _ = this;
    this.chart = new Highcharts.Chart(_.getChartOptions(), function (chart) {
        _.onChartLoad();
    });
};

Analyse.prototype.initData = function (jsonData) {
    var _ = this;
    if (jsonData.length > 0) {
        var d = jsonData;
        if (_.container === "AQI" || _.container === "CO") {
            for (var i = 0; i < jsonData.length; i++) {
                _.monitorValue.push(d[i].MonitorValue);
                _.pollutantName.push(d[i].PollutantName);
                _.timePoint.push(_.formatDate(_.GetTimeFromJsonString(d[i].TimePoint), "MM月DD日"));
            }
        }
        else {      //其它污染物要乘以1000
            for (var i = 0; i < jsonData.length; i++) {
                _.monitorValue.push(d[i].MonitorValue);
                _.pollutantName.push(d[i].PollutantName);
                _.timePoint.push(_.formatDate(_.GetTimeFromJsonString(d[i].TimePoint), "MM月DD日"));
            }

        }
        _.createChart();
    }
    else {
        return "no data";
    }
};

Analyse.prototype.formatDate = function (date, format) {
    if (arguments.length < 2 && !date.getTime) {
        format = date;
        date = new Date();
    }
    typeof format != 'string' && (format = 'YYYY年MM月DD日 hh时mm分ss秒');
    var week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', '日', '一', '二', '三', '四', '五', '六'];
    return format.replace(/YYYY|YY|MM|DD|hh|mm|ss|星期|周|www|week/g, function (a) {
        switch (a) {
            case "YYYY": return date.getFullYear();
            case "YY": return (date.getFullYear() + "").slice(2);
            case "MM": return date.getMonth() + 1;
            case "DD": return date.getDate();
            case "hh": return date.getHours();
            case "mm": return date.getMinutes();
            case "ss": return date.getSeconds();
            case "星期": return "星期" + week[date.getDay() + 7];
            case "周": return "周" + week[date.getDay() + 7];
            case "week": return week[date.getDay()];
            case "www": return week[date.getDay()].slice(0, 3);
        }
    });
}

//将json时间转换成javascript时间

Analyse.prototype.GetTimeFromJsonString = function (jsonTime) {
    var re = /-?\d+/;
    var m = re.exec(jsonTime);
    var d = new Date(parseInt(m[0]));
    return d;
}